package com.shuqi.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.shuqi.activity.adapter.CancleBuyAdapter;
import com.shuqi.activity.viewport.CommonTitle;
import com.shuqi.controller.R;
import com.shuqi.database.dao.impl.BookInfoDao;
import com.shuqi.database.model.BookInfo;
import com.shuqi.model.manager.MyAccountManager;

/**
 * 购买状态
 * 
 * @author zhangpeng
 * 
 */
public class CancleBuyActivity extends ActivityBase implements OnClickListener, OnItemClickListener {
    private ListView listView;
    private List<BookInfo> bookInfos;
    private CancleBuyAdapter adapter;
    private LinearLayout noneLayout;
    private TextView delView;
    private String uid;
    private Map<String, BookInfo> selectBooks;
    private String strCancelAutoBuy = "";
    private String strCancelAutoBuyTitle = "";
    private CommonTitle mTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_canclebuy);
        activityLogicForward();
    }

    @Override
    public void activityLogicForward() {
        mTitle = (CommonTitle) findViewById(R.id.title);
        mTitle.setLeftClickListener(this);
        strCancelAutoBuy = getResources().getString(R.string.cancel_auto_buy_text);
        
        selectBooks = new HashMap<String, BookInfo>();
        adapter = new CancleBuyAdapter(this);
        
        listView = (ListView) findViewById(R.id.listview);
        noneLayout = (LinearLayout) findViewById(R.id.none_layout);
        delView = (TextView) findViewById(R.id.del_view);
        delView.setBackgroundResource(R.drawable.cancel_autu_btn_d);
        delView.setEnabled(false);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        delView.setOnClickListener(this);
        uid = MyAccountManager.getInstance(getApplicationContext()).getUserId();
        activityInitData();
    }

    @Override
    public void activityInitData() {
        bookInfos = BookInfoDao.getInstance().getAllAutoBuyBook(uid);
        selectBooks.clear();

        if (bookInfos == null || bookInfos.size() == 0) {
            noneLayout.setVisibility(View.VISIBLE);
            listView.setVisibility(View.GONE);
            delView.setEnabled(false);
            delView.setText(strCancelAutoBuy);
        } else {
            noneLayout.setVisibility(View.GONE);
            listView.setVisibility(View.VISIBLE);
            adapter.setData(bookInfos);
            adapter.setSelectMap(selectBooks);
            adapter.notifyDataSetChanged();
            delView.setEnabled(true);
            delView.setText(strCancelAutoBuy);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left:
                ActivityManager.getInstance().finishActivity(this);
                break;
            case R.id.del_view:
                if (selectBooks != null) {
                    Set<String> set = selectBooks.keySet();
                    for (String string : set) {
                        BookInfoDao.getInstance().updateAutoBuyBookState(string, null, uid,
                                BookInfo.NO_AUTO_BUY);
                    }
                    activityInitData();
                    delView.setBackgroundResource(R.drawable.cancel_autu_btn_d);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        BookInfo info = bookInfos.get(position);
        if (info != null) {
            if (selectBooks.containsKey(info.getBookId())) {
                selectBooks.remove(info.getBookId());
            } else {
                selectBooks.put(info.getBookId(), info);
            }
            refreshUIOnSelectedChanged();
        }
    }
    private void refreshUIOnSelectedChanged(){
        int num = selectBooks==null?0:selectBooks.size();
        if (num == 0) {
            delView.setBackgroundResource(R.drawable.cancel_autu_btn_d);
            delView.setText(strCancelAutoBuy);
            delView.setEnabled(false);
        } else {
            delView.setBackgroundResource(R.drawable.common_btn_green);
            delView.setEnabled(true);
            delView.setText(strCancelAutoBuy + "(" + num + ")");
        }
        if(adapter!=null){
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(selectBooks!=null && selectBooks.size()>0){
                selectBooks.clear();
                refreshUIOnSelectedChanged();
            }else{
                ActivityManager.getInstance().finishActivity(this);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
